/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.ref;

import java.lang.reflect.*;
import java.util.Date;

/**
 * <p>Project: app2022 - R4
 * <p>Powered by webrx On 2022-02-09 15:55:21
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class R4 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        var u = User.class;
        //Method
        for (Method m : u.getDeclaredMethods()) {
            //System.out.println(m.getName());
        }

        //Constructor Method
        for (Constructor<?> c : u.getConstructors()) {
            //System.out.println(c.getName() + c.getParameterCount());
        }

        //实例化
        var o = u.getConstructor().newInstance();  //new User();

        //new User(100, "李建", new Date(), 10d);
        var o2 = u.getConstructor(int.class, String.class, Date.class, double.class).newInstance(100, "李建", new Date(), 10d);
        var mmm = u.getMethod("getName");

        System.out.println(mmm.invoke(o)); //o.getName();
        System.out.println(mmm.invoke(o2));//o2.getName();

        //-----------属性
        for (Field f : u.getDeclaredFields()) {
            System.out.println(f.getName());//id
            System.out.println(f.getModifiers());//2 private
            System.out.println(Modifier.toString(f.getModifiers()));//private
            System.out.println(f.getType().getName());//int
        }


        //
        var ls = u.getConstructor().newInstance();
        ls.setName("李四");
        System.out.println(ls.getName());

    }
}
